<?php
// +----------------------------------------------------------------------
// | 中犇单商户
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2021 中犇科技 All rights reserved.
// +----------------------------------------------------------------------


namespace app\api\controller\v1;

use app\api\controller\Base;
use app\goods\model\Goods as GoodsModel;
use app\user\model\Task as TaskModel;
use service\ApiReturn;
use Think\Db;


/**
 * APP首页数据展示接口
 * @package app\api\controller\v1
 */
class Index extends Base
{
    /*
     * 判断是否是新用户
     * */
    public function isNew($data, $user)
    {
        $orders = Db::name("order")->where(['user_id' => $user['id'], 'pay_status' => 1])->count();
        if(!$orders){
             return ApiReturn::r(0, [], lang('不是新用户')); 
        }   
        return ApiReturn::r(1, [], lang('成功'));
    }

    /**
     * 获取商品的活动版块
     * @param $type int 商品版块 [0=>is_recommed, 1=>is_new, 2=>is_hot]
     * @param $size int 返回数量
     * @author 刘明美 [ liumingmei@zhongbenjituan.com ]
     */
    public function goods_block($data)
    {
        if ($data['goods_id']) {
            $goods_id_list = explode(',', $data['goods_id']);
            $map[] = ['g.id', 'in', $goods_id_list];
        } else {
            $typeWhere = [['g.is_recommend', '=', 1], ['g.is_new', '=', 1], ['g.is_hot', '=', 1], ['g.is_boutique', '=', 1], ['g.is_sale', '=', 1]];
            $type = $data['type'] ?: 0;
            if ($type == 3) {
                $order = ['weight' => 'desc'];
            }
            $map[] = $typeWhere[$type];
        }
        if ($data['cid']) {
            $ids = \app\goods\model\Category::getChildsId( $data['cid']);
            $map[] = ['g.cid', 'in', $ids];
        }
        $total = GoodsModel::alias("g")->where($map)->where([['g.is_delete', '=', 0], ['g.is_sale', '=', 1], ['g.status', '=', 1]])->count();
        $goods = GoodsModel::goods_list($map, $order, $data['size'], $data['page']);
        

        return ApiReturn::r(1, ['data' => $goods, 'current_page' => $data['page'], 'last_page' => ceil($total / $data['size'])], lang('商品版块'));
    }


    /**
     * 获取任务相关数据
     * @author jxy [ 415782189@qq.com ]
     */
    public function task($data, $user)
    {
        $task = TaskModel::name('user_task')->select();
        foreach ($task as $k => $v) {
            switch ($v['sign']) {
                case 'bindWx':
                    $is_used = Db::name('user_task_log')->where(['tid' => $v['id'], 'uid' => $user['id']])->find();
                    break;
                case 'firstSign':
                    $is_used = Db::name('user_signin')->where(['user_id' => $user['id']])->find();
                    break;
                case 'firstOrder':
                case 'browseGoods':
                case 'shareGoods':
                    $is_used = Db::name('user_task_log')->where(['tid' => $v['id'], 'uid' => $user['id']])->whereTime('create_time', 'today')->find();
                    break;

            }
            $task[$k]['done'] = $is_used ? 1 : 0;
        }
        return ApiReturn::r(1, $task, lang('任务信息'));
    }


}
